$( document ).ready(function() {
    var max_selects = 3;
    var selection_div = $('#genre-selection');
    var select_options = $('#form_Genre_1')[0];
    
    $('#add-genre').click(function() {
        var selects_created = selection_div.find('select').length;
        if(selects_created < max_selects) {
            var new_select = $('<select>')
                .attr('id', 'form_Genre_' + (selects_created+1))
                .attr('name', 'Genre_' + (selects_created+1))
                .addClass('form-control')
                .insertAfter('#form_Genre_'+selects_created);
        
            for(var current_option = 0; current_option < select_options.length; current_option++) {
                new_select.append($("<option>").attr('value',select_options[current_option].value).text(select_options[current_option].innerText));
            }
            
            if(selects_created+1 === max_selects) {
                $('#add-genre').prop('disabled', true);
            }
            
            if(selects_created + 1 > 1) {
               $('#remove-last-genre').prop('disabled', false); 
            }
        } 
    });
    
    $('#remove-last-genre').click(function() {
        var selects_created = selection_div.find('select').length;
        if(selects_created > 1) {
            $('#form_Genre_'+selects_created).remove();
            $('#add-genre').prop('disabled', false);
        }
        
        if(selects_created-1 === 1) {
            $('#remove-last-genre').prop('disabled', true);
        }
    });
});
